package com.siqi.hr.system.custom.service;

import com.siqi.hr.common.utils.Const;
import com.siqi.hr.system.common.enums.OperationEnum;
import com.siqi.hr.system.custom.dao.intf.EntityOperationDao;
import com.siqi.hr.system.custom.entity.EntityOperation;
import com.siqi.hr.system.custom.entity.EntityRecord;
import com.siqi.hr.system.custom.filter.EntityOperationFilter;
import com.siqi.hr.system.custom.utils.EntityOperationUtils;
import com.siqi.hr.system.organization.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实体权限操作Service层(主要处理操作按钮的增删改查)
 * Created by Administrator on 2016/8/16.
 * @author xiaogq
 * @since JDK1.8.0_12
 */
@Service("entityOperationService")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class EntityOperationService {

    @Autowired
    private EntityOperationDao entityOperationDao;

    /**
     * 获取实体操作表列表显示字段(xiaogq)
     *@return map
     */
    public Map<String, Object> queryEntityOperationListColumns() {
        Map<String, Object> columnMap = new HashMap<String, Object>();
        StringBuffer columns = new StringBuffer();
        columns.append("<th data-field='position' data-checkbox='true'></th>");
        columns.append("<th data-field='operation' data-formatter='showColOperator'>操作</th>");
        columns.append("<th data-field='name' data-sortable='true'>操作名称</th>");
        //columns.append("<th data-field='operationType'>操作类型</th>");
        columns.append("<th data-field='sysOperation'>系统操作</th>");
        columns.append("<th data-field='isHere'>显示位置</th>");
        columns.append("<th data-field='description'>描述</th>");
        columnMap.put("fields", "");
        columnMap.put("columns", columns);
        return columnMap;
    }

    /**
     * 根据条件查询所有的操作(xiaogq)
     * operation      需要添加的操作对象
     */
    public Map<String,Object> queryAllOperation(EntityOperationFilter operationFilter) throws Exception {
        Map<String,Object> resultMap = new HashMap<String,Object>();
        //根据条件查询列表数据
        List<EntityOperation> operationList = entityOperationDao.queryAllOperation(operationFilter);
        //根据条件查询总记录数
        int totals = entityOperationDao.queryAllOperationCounts(operationFilter);
        //最终映射表格数据集合
        List<Map<String, Object>> resultDatas = new ArrayList<Map<String, Object>>();
        for(EntityOperation oper : operationList){
            Map<String,Object> dataMaps = new HashMap<String,Object>();
            List<EntityOperation> optionList = new ArrayList<EntityOperation>();
            //所有实体默认都有编辑按钮
            EntityOperation entityEdit = new EntityOperation("编辑","entityOperationUpdate","EntityOperation/initEditOperation.do","glyphicon glyphicon-edit",OperationEnum.showColumn.name(),new EntityRecord(0));
            optionList.add(entityEdit);
            if(!oper.isSysOperation()){
                EntityOperation entityDel = new EntityOperation("删除","entityOperationDelete","EntityOperation/delOperation.do","glyphicon glyphicon-trash",OperationEnum.showColumn.name(),new EntityRecord(0));
                optionList.add(entityDel);
            }
            dataMaps.put("id",oper.getId());
            dataMaps.put("position",oper.getPosition());
            dataMaps.put("operation",optionList);
            dataMaps.put("name",oper.getName());
            //dataMaps.put("operationType", EntityOperationUtils.getOperationType(oper.getOperationType()));
            dataMaps.put("sysOperation",oper.isSysOperation() == null || !oper.isSysOperation() ? "否" : "是");
            dataMaps.put("isHere",EntityOperationUtils.getButtonShowHere(oper));
            dataMaps.put("description",oper.getDescription());
            resultDatas.add(dataMaps);
        }
        resultMap.put(Const.ROWS, resultDatas);
        resultMap.put(Const.TOTAL, totals);

        return resultMap;
    }

    /**
     * 根据条件查询所有操作记录总数(xiaogq)
     * operation      需要添加的操作对象
     */
    public int queryAllOperationCounts(EntityOperationFilter operationFilter) throws Exception {
        return entityOperationDao.queryAllOperationCounts(operationFilter);
    }

    /**
     * 根据当前角色查询实体指定位置的操作(xiaogq)
     * @param role                 角色对象信息
     * @param entityId            实体id
     * @param operationEnum      按钮显示的位置枚举值(showList：显示在列表，showColumn：显示在列前，showView:显示在查看页)
     * @return
     */
    public List<EntityOperation> queryEntityOperationByHere(Role role, int entityId,OperationEnum operationEnum)throws Exception{
        return entityOperationDao.queryEntityOperationByHere(role.getId(),entityId,operationEnum);
    }

    /**
     * 根据操作id查询对应的操作信息(xiaogq)
     * @param id                   操作id
     */
    public EntityOperation queryEntityOperationById(String id) throws Exception {
        return entityOperationDao.queryEntityOperationById(id);
    }

    /**
     * 添加操作接口方法(xiaogq)
     * operation      需要添加的操作对象
     */
    public int addEntityOperation(EntityOperation operation) throws Exception {
        return entityOperationDao.addEntityOperation(operation);
    }

    /**
     * 修改操作接口方法(xiaogq)
     * operation      需要添加的操作对象
     */
    public int updateEntityOperation(EntityOperation operation) throws Exception {
        return entityOperationDao.updateEntityOperation(operation);
    }

    /**
     * 根据id删除操作方法(xiaogq)
     * @param id                   操作id
     */
    public void deleteEntityOperation(String id) throws Exception {
        entityOperationDao.deleteEntityOperation(id);
    }

}
